package ms.server.api.bean.vo;


import ms.server.api.util.JacksonUtils;
import lombok.Data;

/**
 * @Author ybc
 * @Date 2021/6/25 11:00
 */
@Data
public class ResultVO<T> {

    private Integer code;

    private String message;

    private T data;

    public ResultVO(Integer code,String message,T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResultVO(ResultCode resultCode, T data) {
        this.code = resultCode.code();
        this.message = resultCode.message();
        this.data = data;
    }

    /**
     * 无数据返回的成功响应
     * @return ResultVO
     */
    public static ResultVO success() {
        return new ResultVO(ResultCode.SUCCESS , null);
    }

    /**
     * 成功响应
     * @param data 响应数据
     * @return ResultVO
     */
    public static ResultVO success(Object data) {
        return new ResultVO(ResultCode.SUCCESS, data);
    }

    /**
     * 异常响应
     * @return ResultVO
     */
    public static ResultVO error() {
        return new ResultVO(ResultCode.ERROR, null);
    }

    @Override
    public String toString() {
        return JacksonUtils.objToJson(this);
    }

}
